package com.rabbitmq.demo.tx;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.transaction.annotation.Transactional;

/**
 * 1. 客户端发请求，将通信信道设置为事务模式
 * 2. 服务端给出答复，同意将信道设置为事务模式
 * 3. 发送消息
 * 4. 提交事务
 * 5. 服务端给出答复，确认事务提交
 */
public class Sender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Scheduled(fixedDelay = 1000,initialDelay = 500)
    @Transactional
    public void send(){
        rabbitTemplate.convertAndSend("direct.exchange","queue","hello! RabbitMQ!");
        int a = 10/0;
    }
}
